package com.spring.boot.standard.admin.controller;

import java.io.InputStream;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import com.spring.boot.standard.admin.common.beans.PageData;
import com.spring.boot.standard.admin.common.CommonController;
import com.spring.boot.standard.admin.common.beans.CommonResult;
import com.spring.boot.standard.admin.entity.Developerconfiguration;
import com.spring.boot.standard.admin.service.DeveloperconfigurationService;

/**
 * @author qinhao
 * @since Mon Sep 06 10:36:14 UTC 2021
 */
@RestController
@RequestMapping("/developerconfiguration")
@Api(value="开发配置接口",tags={"developerconfiguration(开发配置)接口"})
public class DeveloperconfigurationController extends CommonController {
	
    @Autowired
    DeveloperconfigurationService developerconfigurationService;

	@ApiOperation(value = "获取分页数据" ,notes = "获取分页数据" )
	@ApiImplicitParams({
		@ApiImplicitParam(name = "key" ,value = "搜索关键字名称" , required = false, dataType = "String"),
		@ApiImplicitParam(name = "keywords" ,value = "搜索关键字内容" , required = false, dataType = "String")
	})
    @RequestMapping(value = "/pagedata", method = { RequestMethod.GET  })
    public PageData<Developerconfiguration> pagedata(Pageable pageable,String key,String keywords){
        return convertPage(developerconfigurationService.findAll(pageable,new Object[]{key,keywords}));
    }

	@ApiOperation(value = "获取单条数据对象" ,notes = "获取单条数据对象")
	@ApiImplicitParams({
		@ApiImplicitParam(paramType = "query",name = "developerconfigurationid" ,value = "开发配置ID" , required = true, dataType = "String")
	})
	@RequestMapping(value = "/singledata" ,method = { RequestMethod.GET })
	public Developerconfiguration singledata(String developerconfigurationid){
		return developerconfigurationService.findById(developerconfigurationid);
	}

	@ApiOperation(value = "删除开发配置", notes = "删除开发配置" )
		@ApiImplicitParams({ @ApiImplicitParam(name = "ids", value = "ID拼接的字符串", required = true, dataType = "String")
	})
	@RequestMapping(value = "/delete" ,method = { RequestMethod.DELETE})
	public CommonResult delete(String ids) {
		try {
			String[] id_array = ids.split(",");
			for(String developerconfigurationid:id_array){
				developerconfigurationService.deleteById(developerconfigurationid);
			}
			cr = new CommonResult(true,0,null,"删除成功");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return cr;
	}

	@ApiOperation(value = "保存开发配置", notes = "保存开发配置,id列为空则为新增,不为空则为修改")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "Developerconfiguration",value = "开发配置",required = false,dataType = "Developerconfiguration")
	})
	@RequestMapping(value = "/save" ,method = { RequestMethod.POST })
	public CommonResult save(Developerconfiguration developerconfiguration) {
		try {
			developerconfigurationService.save(developerconfiguration);
			cr = new CommonResult(true,0,null,"保存成功");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return   cr;
	}

	@ApiOperation(value = "导出数据", notes = "导出数据")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "key" ,value = "搜索关键字名称" , required = false, dataType = "String"),
		@ApiImplicitParam(name = "keywords" ,value = "搜索关键字内容" , required = false, dataType = "String")
	})
	@RequestMapping(value = "/exports", method = { RequestMethod.GET  })
	public ResponseEntity<byte[]> exports( Pageable pageable,String key,String keywords) {
		ResponseEntity<byte[]> entity = null;
	    String[] titleNameArray = {"应用服务组件代码目录","前端代码目录@前端代码目录","数据库访问路径@数据库访问路径","数据库用户@数据库用户","数据库密码@数据库密码","数据库端口号@数据库端口号","数据库表格@数据库表格","用户id@用户id","项目名称@项目名称"};
		String[] fieldNameArray = {"servercodelist","clientcodelist","mysqlhost","mysqluser","mysqlpassword","mysqlport","mysqldatabase","userid","projectname"};
		try {
			//根据条件获取数据
            List<Developerconfiguration> data = developerconfigurationService.findAll(pageable,new Object[]{key,keywords}).getContent();
			//数据转换成流并导出
			InputStream is = super.exportExcelContent(data,titleNameArray,fieldNameArray);
			byte[] body = new byte[is.available()];
			is.read(body);
			HttpHeaders headers = new HttpHeaders();
			String exportFilename = new SimpleDateFormat("yyyyMMddhhmmssSSS").format(new Date()) + ".xls";
			headers.add("Content-Disposition", "attchement;filename=" + exportFilename);
			HttpStatus statusCode = HttpStatus.OK;
			entity = new ResponseEntity<byte[]>(body, headers, statusCode);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return entity;
	}

	@ApiOperation(value = "导入开发配置", notes = "导入开发配置")
    @RequestMapping(value = "/imports", method = { RequestMethod.POST })
	public CommonResult imports(@RequestParam("file") MultipartFile file) {
		int imp_num = 0;
		//如果文件不为空，写入上传路径
		try {
			if(!file.isEmpty()) {
				String[] fieldNameArray = {"servercodelist","clientcodelist","mysqlhost","mysqluser","mysqlpassword","mysqlport","mysqldatabase","userid","projectname"};
				List<Map<String,String>> list = super.getExcelContent(file, fieldNameArray);
                imp_num =  developerconfigurationService.saveFromList(list);
				cr = new CommonResult(true,0,null,"导入成功，导入数据："+imp_num+"条！");
			} else {
				cr = new CommonResult(false,500,null,"文件上传失败！");
			}
		} catch (Exception e) {
			cr = new CommonResult(false,500,null,super.getPointOfException(e.getMessage()));
		}
		return  cr;
	}

}